package com.xjtu.duansheli.controller;


import com.xjtu.duansheli.entity.CategoriesInfo;
import com.xjtu.duansheli.entity.ProductImagesInfo;
import com.xjtu.duansheli.entity.ProductInfo;
import com.xjtu.duansheli.entity.UserInfo;
import com.xjtu.duansheli.json.AjaxResponse;
import com.xjtu.duansheli.service.CategoriesInfoService;
import com.xjtu.duansheli.service.ProductImagesInfoService;
import com.xjtu.duansheli.service.ProductInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */

@Slf4j
@RestController
@Api(value = "商品信息管理模块", description = "这是商品信息增删改查的模块")
@RequestMapping("/project/product-info")
public class ProductInfoController {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private CategoriesInfoService categoriesInfoService;

    @Autowired
    private ProductImagesInfoService productImagesInfoService;

    @ApiOperation(value = "获取全部商品", notes = "获取所有商品的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getProducts", method = RequestMethod.GET)
    public AjaxResponse getAll(@RequestParam(value = "current") Integer current,
                               @RequestParam(value = "size") Integer size) {
        List<ProductInfo> productInfoList = productInfoService.getAllProduct(current,size);
        Map<Object,Object> m = new HashMap<>();
        for (CategoriesInfo c:categoriesInfoService.getAllCategories(1,7)) {
            List<Object> list1 = new ArrayList<>();
            for (ProductInfo p: productInfoList) {
                if(!p.getCategoryId().equals(c.getCid())){
                    continue;
                }
                Map<Object, Object> res = new HashMap<Object, Object>();
                res.put("goodId",p.getPid());
                Map<Object, Object> marker = new HashMap<Object, Object>();
                marker.put("addr_name",p.getAddrName());
                marker.put("address",p.getAddress());
                marker.put("longitude",p.getLongitude());
                marker.put("latitude",p.getLatitude());
                res.put("marker",marker);
                res.put("name",p.getPname());
                res.put("price",p.getPrize());
                List<ProductImagesInfo> productImagesInfo = productImagesInfoService.getProductImageBypid(p.getPid());
                List<String> urls = new ArrayList<String>();
                for (ProductImagesInfo pI: productImagesInfo) {
                    urls.add(pI.getImageUrl());
                }
                res.put("url",urls);
                list1.add(res);
            }
            m.put(c.getCname(),list1);
        }

        return AjaxResponse.success(m);
    }


    @ApiOperation(value = "根据id查找商品", notes = "获取根据id查找到的商品")
    @ApiImplicitParam(name = "pid", value = "商品id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getProductById", method = RequestMethod.GET)
    public AjaxResponse getProductById(@RequestParam(name = "pid") Long pid) {
        ProductInfo p = productInfoService.getProductByid(pid);
        Map<Object, Object> res = new HashMap<Object, Object>();
        res.put("goodId",p.getPid());
        Map<Object, Object> marker = new HashMap<Object, Object>();
        marker.put("addr_name",p.getAddrName());
        marker.put("address",p.getAddress());
        marker.put("longitude",p.getLongitude());
        marker.put("latitude",p.getLatitude());
        res.put("marker",marker);
        res.put("name",p.getPname());
        res.put("price",p.getPrize());
        List<ProductImagesInfo> productImagesInfo = productImagesInfoService.getProductImageBypid(p.getPid());
        List<String> urls = new ArrayList<String>();
        for (ProductImagesInfo pI: productImagesInfo) {
            urls.add(pI.getImageUrl());
        }
        res.put("url",urls);
        return AjaxResponse.success(res);
    }

    @ApiOperation(value = "根据关键字搜索商品", notes = "获取根据关键字搜索到的商品列表")
    @ApiImplicitParam(name = "keyWords", value = "搜索关键字", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/searchProductsByKey", method = RequestMethod.GET)
    public AjaxResponse searchProductsByKey(@RequestParam(name = "keyWords") String keyWords) {
        return AjaxResponse.success(productInfoService.searchProductByKey(keyWords));
    }

    @ApiOperation(value = "根据卖家id查找它发布的商品", notes = "根据卖家id查找他发布的所有商品")
    @ApiImplicitParam(name = "saleid", value = "卖家id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getProductBysaleId", method = RequestMethod.GET)
    public AjaxResponse getProductBysaleId(@RequestParam(name = "saleid") Long saleid) {
        return AjaxResponse.success(productInfoService.getProductBysaleid(saleid));
    }


    @ApiOperation(value = "添加商品信息", notes = "获取商品信息并且添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pName", value = "商品名", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "prize", value = "商品价格", required = true, paramType = "form", dataType = "double"),
            @ApiImplicitParam(name = "amount", value = "商品数量", required = true, paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "describes", value = "商品描述", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "category_id", value = "商品所属类别id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "addrName", value = "商品地址名", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "address", value = "商品详细地址", required = false, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "longitude", value = "商品经度", required = true, paramType = "form", dataType = "Float"),
            @ApiImplicitParam(name = "latitude", value = "商品纬度", required = true, paramType = "form", dataType = "Float")
    })
    @RequestMapping(value = "/addProduct", method = RequestMethod.POST)
    public AjaxResponse addProduct(@RequestParam(value = "pName") String pName,
                                @RequestParam(value = "prize") Double prize,
                                @RequestParam(value = "amount") Integer amount,
                                @RequestParam(value = "describes") String describes,
                                @RequestParam(value = "category_id") Long cId,
                                @RequestParam(value = "addrName") String addrName,
                                @RequestParam(value = "address") String address,
                                @RequestParam(value = "longitude") Float longitude,
                                @RequestParam(value = "latitude") Float latitude,
                                HttpServletRequest request) {
        UserInfo user = (UserInfo) request.getSession().getAttribute("loginUser");
        ProductInfo product = new ProductInfo()
                .setPname(pName)
                .setPrize(prize)
                .setAmount(amount)
                .setDescribes(describes)
                .setCategoryId(cId)
//                .setSaleId(user.getUid())
                .setAddress(address)
                .setAddrName(addrName)
                .setLatitude(latitude)
                .setLongitude(longitude).setSaleId(1322451812704079874L);
        return AjaxResponse.success("数据库受影响行数->" + productInfoService.addProduct(product));
    }

    @ApiOperation(value = "修改商品信息", notes = "录入新的商品信息并且修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pId", value = "需要修改的商品id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "pName", value = "商品名", required = false, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "prize", value = "商品价格", required = false, paramType = "form", dataType = "double"),
            @ApiImplicitParam(name = "amount", value = "商品数量", required = false, paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "describes", value = "商品描述", required = false, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "category_id", value = "商品所属类别id", required = false, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "addrName", value = "商品地址名", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "address", value = "商品详细地址", required = false, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "longitude", value = "商品经度", required = true, paramType = "form", dataType = "Float"),
            @ApiImplicitParam(name = "latitude", value = "商品纬度", required = true, paramType = "form", dataType = "Float")
    })
    @RequestMapping(value = "/updateProduct", method = RequestMethod.PUT)
    public AjaxResponse updateProduct(@RequestParam(value = "pId") Long pId,
                                      @RequestParam(value = "pName") String pName,
                                      @RequestParam(value = "prize") Double prize,
                                      @RequestParam(value = "amount") Integer amount,
                                      @RequestParam(value = "describes") String describes,
                                      @RequestParam(value = "category_id") Long cId,
                                      @RequestParam(value = "addrName") String addrName,
                                      @RequestParam(value = "address") String address,
                                      @RequestParam(value = "longitude") Float longitude,
                                      @RequestParam(value = "latitude") Float latitude) {

        ProductInfo product = new ProductInfo()
                .setPid(pId)
                .setPname(pName)
                .setPrize(prize)
                .setAmount(amount)
                .setDescribes(describes)
                .setCategoryId(cId)
                .setAddress(address)
                .setAddrName(addrName)
                .setLatitude(latitude)
                .setLongitude(longitude);
        return AjaxResponse.success("数据库受影响行数->" + productInfoService.updateProduct(product));
    }

    @ApiOperation(value = "删除商品信息", notes = "从数据库中删除某个商品")
    @ApiImplicitParam(name = "pid", value = "删除商品id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteProduct", method = RequestMethod.DELETE)
    public AjaxResponse deleteProduct(@RequestParam(value = "pid") Long pid) {
        return AjaxResponse.success("数据库受影响行数->" + productInfoService.deleteProduct(pid));
    }

    @ApiOperation(value = "商品总数量", notes = "获取数据库中商品总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(productInfoService.getTotal());
    }

}

